package com.c0nflict.server;

import java.sql.Timestamp;
import java.util.Date;

public class GameStats {	
	private int numPlayers = 0;
	private Timestamp timeStamp = null;
	private int tableId = 0;
	private int firstPlayerId = -1;
	private int winningPlayerId = -1;
	private int turns = 0;
	private Date gameStart = null;
	private Date gameEnd = null;
	private int dbIds[] = null;
	private int elo[] = null;
	private int eloChange[] = null;
	private int finishPlace[] = null;
	private Date deathTime[] = null;
	private int totalPlayers = 0;
	
	
	public void setTotalPlayers(int num) { totalPlayers = num; }
	
	public void addLuckMetric(int playerId,int aval, int dval, int outcome) {
		
	}
	public void addTotalEarned(int playerId, int earned) {
		
	}
	
	
	public void setTableId(int id) { tableId = id; }
	public void setFirstPlayerId(int id) { firstPlayerId = id; }
	public void setWinningPlayerId(int id) { winningPlayerId = id; }
	public void setGameTurns(int turns) { this.turns = turns; }
	public void setPlayerDbId(int playerId, int dbId) { 
		//if (dbId<0) dbId=0;
		try{ dbIds[playerId] = dbId; 		} catch (Exception e) {
		System.out.print("GAMESTATERROR");
		e.printStackTrace();
	}
}
	public void setPlayerELO(int playerId, int elo) { try{ this.elo[playerId] = elo; 		} catch (Exception e) {
		System.out.print("GAMESTATERROR");
		e.printStackTrace();
	}
} 
	public void setPlayerELOChange(int playerId, int eloChange) { try{ this.eloChange[playerId] = eloChange;		} catch (Exception e) {
		System.out.print("GAMESTATERROR");
		e.printStackTrace();
	}
 }
	public void setPlayerFinishPlace(int playerId, int place) { try{ finishPlace[playerId] = place;		} catch (Exception e) {
		System.out.print("GAMESTATERROR");
		e.printStackTrace();
	}
 }
	public void setPlayerDeathTime(int playerId, Date time) { try { deathTime[playerId] = time; 		} catch (Exception e) {
		System.out.print("GAMESTATERROR");
		e.printStackTrace();
	}
}
	public void endGame() { gameEnd = new Date(); }
	
	public int getLastInsertId() {
		String query = "select last_insert_id() as id;";
		return new Integer(tables.singleValueSql(query)).intValue();
	}
	
	// Write results to database
	public void save() {
		try {
		//this function doesnt work. 
		if (gameStart == null)
			return;
		
		// Write summary line
		try {
			String query = "insert into dq_game_stats_summary values (null, " + tableId +  ", " + firstPlayerId + ", " +
				winningPlayerId + ", " + turns + ", ";
			
			timeStamp = new Timestamp(gameStart.getTime());
			query += "'" + timeStamp.toString() + "', ";
			
			timeStamp = new Timestamp(gameEnd.getTime());
			query += "'" + timeStamp.toString() + "');";
			tables.updateSQL(query);
			int game_id = getLastInsertId();
			
			// Write player details
			for (int i = 0; i < numPlayers; i++) {
				if (dbIds[i]>0) {
					timeStamp = new Timestamp(deathTime[i].getTime());
					query = "insert into dq_game_stats_players values (" + game_id + ", " + dbIds[i] + ", " + i + ", " +
						finishPlace[i] + ", " +	elo[i] + ", " +	eloChange[i] + ", '" + timeStamp.toString() + "');";
	
					tables.updateSQL(query);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		} catch (Exception e) {
			System.out.print("GAMESTATERROR");
			e.printStackTrace();
		}
	}
	
	private TableManager tables;
	
	public GameStats(int playerCount, TableManager t) {
		tables = t;
		numPlayers = playerCount;
		dbIds = new int[numPlayers];
		elo = new int[numPlayers];
		eloChange = new int[numPlayers];
		finishPlace = new int[numPlayers];
		deathTime = new Date[numPlayers];
		
		gameStart = new Date();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
